<template>
    <el-header style="height: 60px;">
        <div class="nav-left">

        </div>
        <div class="nav-right">
            <el-dropdown @command="handleCommand">
        <span class="el-dropdown-link">
          <i class="iconfont icon-profile"></i> <span class="xs-hidden">{{authuser.username}}</span> <i
                class="el-icon-arrow-down el-icon--right"></i>
        </span>
                <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item command="changePassword">修改密码</el-dropdown-item>
                    <el-dropdown-item command="logout">离开</el-dropdown-item>
                </el-dropdown-menu>
            </el-dropdown>
        </div>

        <el-dialog title="修改登录密码" :visible.sync="dialogFormVisible" width="400px">
            <el-form :model="passForm" ref="passForm" :rules="passRules" label-width="100px">
                <el-form-item label="旧密码" prop="old_pass">
                    <el-input type="password" v-model="passForm.old_pass" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="新密码" prop="new_pass">
                    <el-input type="password" v-model="passForm.new_pass" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="确认新密码" prop="check_pass">
                    <el-input type="password" v-model="passForm.check_pass" auto-complete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="changePassword" :loading="loading">确 定</el-button>
            </div>
        </el-dialog>

    </el-header>
</template>
<style>
</style>
<script>
  export default {
    data() {

      let validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入新密码'));
        } else {
          if (this.passForm.check_pass !== '') {
            this.$refs.passForm.validateField('check_pass');
          }
          callback();
        }
      };
      let validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入新密码'));
        } else if (value !== this.passForm.new_pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };

      return {
        headerActive: window.asideActive,
        menus: window.menus,
        authuser:window.authuser,
        passRules: {
          old_pass: [
            { required: true, trigger: 'blur', message: '请输入旧密码'}
          ],
          new_pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          check_pass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
        },

        loading: false,
        dialogFormVisible: false,
        passForm: {
          old_pass: '',
          new_pass: '',
          check_pass: '',
        }

      };
    },
    created() {

    },
    methods: {
      handleSelect() {
        //
      },
      redirectTo(url) {
        this.$redirectTo(url);
      },
      handleCommand(cmd) {
        if (cmd === 'logout') {
          this.$redirectTo(path + '/logout');
        }
        if (cmd === 'changePassword') {
          this.dialogFormVisible = true;
          this.old_pass = '';
          this.new_pass ='';
          this.check_pass = '';
        }
      },

      changePassword() {

        this.$refs.passForm.validate((valid) => {
          if (valid) {
            this.loading = true;
            axios.post(path + '/change_password', this.passForm).then(res => {
              if (res.data.code === 0) {
                this.dialogFormVisible = false;
                this.$message.success('新密码已生效，下次登录将使用新密码');
              } else {
                this.$message.error(res.data.msg);
              }
              this.loading = false;
            }).catch(() => {
              this.$message.error('系统错误');
              this.loading = false;
            })
          } else {
            return false;
          }
        });
      }
    }
  }
</script>